package org.ust.analyse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;

public class AnalysisUtil
{

	public static ClassInspector	inspector	= new ClassInspector ( );

	public static ClassInspector analyze ( String pathToSourceFolder ,
			String sourceFileName )
	{
		return analyze ( new File ( pathToSourceFolder + sourceFileName ) );
	}

	public static ClassInspector analyze ( File sourceFile )
	{
		BufferedReader bufferedReader;
		StringWriter stringWriter = null;
		try
		{
			bufferedReader = new BufferedReader ( new InputStreamReader (
					new FileInputStream ( sourceFile ) ) );
			stringWriter = new StringWriter ( );
			String line = null;
			while ( ( line = bufferedReader.readLine ( ) ) != null )
			{
				stringWriter.write ( line );
			}
			if ( bufferedReader != null )
			{
				bufferedReader.close ( );
			}
		}
		catch ( FileNotFoundException e )
		{
			e.printStackTrace ( );
		}
		catch ( IOException e )
		{
			e.printStackTrace ( );
		}
		char [ ] unit = stringWriter.toString ( ).toCharArray ( );

		ASTParser parser = ASTParser.newParser ( AST.JLS3 );
		parser.setKind ( ASTParser.K_COMPILATION_UNIT );

		parser.setSource ( unit );
		parser.setResolveBindings ( true );
		CompilationUnit compilationUnit = ( CompilationUnit ) parser
				.createAST ( null );
		getInspector ( ).process ( compilationUnit );
		return getInspector ( );
	}

	public static ClassInspector getInspector ( )
	{
		return inspector;
	}
}
